<?php

/**
 * @author	barbarosalcin
 * @desc	page_admin_clinics
 * @version	$Id: 20110328
 * @package	admin
 */

if ($tk->session->data['user_level'] != ADMIN)
{
	$allowed = 0;
	return;
}

$error = 0;
$filename = basename(__FILE__);
$pagename = substr(substr($filename, 0, - 4), 5, 99);
define('IN_ADMINPAGE', true);

$item_table = WORLDWIDE_TABLE;

/**
 * @ignore createtime,status
 */

$item_fields = "title,intro,content";

/**
 * @uses text/imagebrowser/textarea/ckeditor/hidden/file/select/date/checkbox/number/readonly_text/file_upload
 */

$item_array = array(
		'id' => 'hidden', 
		'title' => 'text', 
		'intro' => 'ckeditor', 
		'content' => 'ckeditor', 
		'arr' => array());


/* default ! do not edit */

$item_fields_additem = preg_replace('/(\w+)/i', '\'\$$1\'', $item_fields);
$item_fields_manitem = preg_replace('/(\w+)/i', '$1=\'\$$1\'', $item_fields);

if ((int) $_POST['submit'])
{
	include ('pages/admin/ajax_' . $pagename . '.php');
	return;
}

/* get list */

if (! empty($setmodules))
{
	$modules[$pagename]['additem'] = $req['language'] . '-' . $pagename . $config['seoext'] . '?act=additem';
	$modules[$pagename]['manitem'] = $req['language'] . '-' . $pagename . $config['seoext'] . '?act=manitem';
	return;
}

if ($req['get']['act'] == 'additem')
{
	/* default ! do not edit */
	$tk->page->_pv['items'] = $item_array;
	$tk->page->_pv['data']['act'] = $req['get']['act'];
	$tk->page->_pv['data'][$req['get']['act']] = $req['language'] . '-' . $pagename . '.html?';
}

if ($req['get']['act'] == 'manitem')
{
	$order_by = "title asc";
	$sql = "SELECT SQL_CACHE id,status,createtime,$item_fields from $item_table order by $order_by";

	$result = $tk->db->sql_query($sql);
	$row = $tk->db->sql_fetchrowset($result);
	
	$tk->page->_pv['data']['list'] = $row;
	
	/* default ! do not edit */
	$tk->page->_pv['items'] = $item_array;
	$tk->page->_pv['data']['act'] = $req['get']['act'];
	$tk->page->_pv['data'][$req['get']['act']] = $req['language'] . '-' . $pagename . '.html?';
	$tk->page->_pv['data_json'] = preg_replace('%\[(.+)\]%', '$1', json_encode($data_json));
}

if ($req['get']['act'] == 'getitem')
{
	$id = (int) $req['get']['id'];
	
	$sql = "SELECT SQL_CACHE id,$item_fields from $item_table where id = '$id'";
	
	$result = $tk->db->sql_query($sql);
	
	$data_json = $tk->db->sql_fetchrow($result);
	
	$tk->page->_pv['json'] = 1;
	$tk->page->_pv['data_json'] = preg_replace('%\[(.+)\]%', '$1', json_encode($data_json));

}

if ($req['get']['act'] == 'delitem')
{
	$id = (int) $req['get']['id'];
	
	$sql = "delete from $item_table where id = '$id'";
	
	if (! ($result = $tk->db->sql_query($sql)))
	{
		$data_json = array(
				'header' => $lang['error_header'], 
				'message' => $lang['error_sql'], 
				'status' => '0');
		$tk->page->_pv['json'] = 1;
		$tk->page->_pv['data_json'] = json_encode($data_json);
	
	}
	else
	{
		$data_json = array(
				'header' => $lang['success_header'], 
				'message' => $lang[$pagename][$req['get']['act']]['success'], 
				'status' => '1');
		$tk->page->_pv['json'] = 1;
		$tk->page->_pv['data_json'] = json_encode($data_json);
	
	}
}

if ($req['get']['act'] == 'toggle')
{
	$id = (int) $req['get']['id'];
	$status = (int) $req['get']['t'];
	
	$sql = "update $item_table set status= '$status' where id='$id'";
	
	if (! ($result = $tk->db->sql_query($sql)))
	{
		$data_json = array(
				'status' => '0');
	}
	else
	{
		$data_json = array(
				'status' => '1');
	}
	
	$tk->page->_pv['json'] = 1;
	$tk->page->_pv['data_json'] = json_encode($data_json);
}
